package com.fzrskj.cloud.free.sys.utils;

import com.fzrskj.cloud.free.sys.bean.SysMiniServiceBean;
import com.fzrskj.cloud.free.sys.dao.SysMiniServiceDao;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import org.apache.commons.collections.map.HashedMap;

import java.util.List;
import java.util.Map;

public class SysMiniServiceUtils extends CObject{
	//private declarations
	private SysMiniServiceDao dao=null;

	//constructor function to initialize database access object
	public SysMiniServiceUtils(){ dao=new SysMiniServiceDao(); }

	//to free resource
	@Override
	public void dispose(){
		if (dao!=null) {
			dao.dispose();
		}
		dao = null;
		super.dispose();
	}

	//new record by map
	public int newSysMiniService(Map<String, Object> values){
		int ret = dao.newSysMiniService(values);
		this.setErrCode(dao.getErrCode());
		return ret;
	}

	//delete record by id
	public int delSysMiniService(int id){
		int ret = dao.delSysMiniService(id);
		this.setErrCode(dao.getErrCode());
		if (ret==0) {
			this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
		}
		return ret;
	}

	//modify record by map
	public int modSysMiniService(Map<String,Object> values, Map<String,Object> cdt){
		int ret = dao.modSysMiniService(values, cdt);
		this.setErrCode(dao.getErrCode());
		if (ret==0) {
			this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
		}
		return ret;
	}

	//query record by id
	public SysMiniServiceBean querySysMiniService(int id){
	    SysMiniServiceBean ret = dao.querySysMiniService(id);
		String err = dao.getErrCode();
		this.setErrCode(err);
		if (err.equals(SysErr.CODE_SUCCESS) && ret==null) {
			this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
		}
		return ret;
	}

	//query record count by cdt
	public int querySysMiniService(Map<String,Object> cdt){
		int ret = dao.querySysMiniService(cdt);
		this.setErrCode(dao.getErrCode());
		return ret;
	}

	//query record list by cdt
	public List<SysMiniServiceBean> querySysMiniService(Map<String,Object> cdt,String orderby, int page,int pagesize){
		List<SysMiniServiceBean> rets = dao.querySysMiniService(cdt,orderby,page,pagesize);
		String err = dao.getErrCode();
		this.setErrCode(err);
		if (err.equals(SysErr.CODE_SUCCESS) && (rets==null || rets.size()==0)){
			this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
		}
		return rets;
	}

	/**
	 *
	 * @param cdt
	 * @param orderBy
	 * @return
	 */
	public List<SysMiniServiceBean> querySysMiniService(Map<String,Object> cdt,String orderBy) {
		List<SysMiniServiceBean> rets = dao.querySysMiniService(cdt, orderBy);
		String err = dao.getErrCode();
		this.setErrCode(err);
		return rets;
	}

	/**
	 * 通过服务编码获取服务信息.
	 * @param serviceCode 服务编码.
	 * @return 服务信息.
	 */
	public SysMiniServiceBean getByCode(String serviceCode) {
		Map<String, Object> cdt = new HashedMap();
		cdt.put("serviceCode", serviceCode);

		SysMiniServiceDao serviceDao = new SysMiniServiceDao();
		List<SysMiniServiceBean> serviceBean = serviceDao.querySysMiniService(cdt, "", 1, 1);
		if (null == serviceBean || serviceBean.isEmpty()) {
			return null;
		}
		serviceDao.dispose();
		return serviceBean.get(0);
	}

	/**
	 * 根据编码查询指定id外的记录
	 * @param serviceCode 服务编码.
	 * @param id id.
	 * @return 服务信息记录.
	 */
	public SysMiniServiceBean getByCodeWithoutId(String serviceCode, Integer id) {
		SysMiniServiceBean serviceBean = this.dao.getByCodeWithoutId(serviceCode, id);
		String err = dao.getErrCode();
		this.setErrCode(err);
		return serviceBean;
	}
}